package com.whut.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.whut.mapper.NewmeasureresulttypeMapper;
import com.whut.mapper.NewmeasuresystemMapper;
import com.whut.model.Newmeasureresulttype;
import com.whut.model.Newmeasuresystem;
import com.whut.service.NewMeasureService;
import com.whut.utils.RangeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.json.JsonObject;
import java.text.DateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service("/newMeasureService")
public class NewMeasureServiceImpl implements NewMeasureService {
    @Resource
    NewmeasuresystemMapper newmeasuresystemMapper;
    @Resource
    NewmeasureresulttypeMapper newmeasureresulttypeMapper;

    private static final String ERROR = "error";
    private static final String SUCCESS = "success";

    @Override
    public Object insertMeasureSystem(String parameterName, Integer preNum, Integer nodeDepth) {
        LocalDateTime now = LocalDateTime.now();
        Newmeasuresystem newmeasuresystem = new Newmeasuresystem(parameterName, preNum, nodeDepth, "N", now, now);
        JSONObject jsonObject = new JSONObject();
        try {
            newmeasuresystemMapper.insertSelective(newmeasuresystem);
            jsonObject.put("operationCode", SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("operationCode", ERROR);
        }
        return jsonObject;
    }

    @Override
    public Object insertMeasureResultOnSix(String parameterName, Integer preNum, Integer nodeDepth) {
        LocalDateTime now = LocalDateTime.now();
        Newmeasureresulttype newmeasureresulttype = new Newmeasureresulttype();
        newmeasureresulttype.setParameterName(parameterName);
        newmeasureresulttype.setPreNum(preNum);
        newmeasureresulttype.setIsDisuse("N");
        newmeasureresulttype.setCreateTime(now);
        newmeasureresulttype.setLastEditTime(now);
        JSONObject jsonObject = new JSONObject();
        try {
            newmeasureresulttypeMapper.insertSelective(newmeasureresulttype);
            jsonObject.put("operationCode", SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("operationCode", ERROR);
        }
        return jsonObject;
    }

    @Override
    public Object updateMeasureSystem(Integer mKey, String parameterName) {
        LocalDateTime now = LocalDateTime.now();
        Newmeasuresystem newmeasuresystem = new Newmeasuresystem(mKey, parameterName, now);
        JSONObject jsonObject = new JSONObject();
        try {
            newmeasuresystemMapper.updateByPrimaryKeySelective(newmeasuresystem);
            jsonObject.put("operationCode", SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("operationCode", ERROR);
        }
        return jsonObject;
    }

    @Override
    public Object disuseMeasureSystem(Integer mKey) {
        LocalDateTime now = LocalDateTime.now();
        Newmeasuresystem n = new Newmeasuresystem();
        n.setmKey(mKey);
        n.setIsDisuse("Y");
        n.setLastEditTime(now);
        JSONObject jsonObject = new JSONObject();
        try {
            newmeasuresystemMapper.updateByPrimaryKeySelective(n);
            jsonObject.put("operationCode", SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("operationCode", ERROR);
        }
        return jsonObject;
    }

    @Override
    public List<Newmeasuresystem> getMeasureSystemByNodeDepth(Integer nodeDepth, Integer preNum) {
        List<Newmeasuresystem> list = newmeasuresystemMapper.getInfoByNodeDepth(nodeDepth, preNum);
        return list;
    }

    @Override
    public Object insertMeasureResult(Newmeasureresulttype newmeasureresulttype) {
        LocalDateTime now = LocalDateTime.now();
        String res = RangeUtil.isRangeCorrect(newmeasureresulttype.getRangeMath());
        newmeasureresulttype.setIsDisuse("N");
        newmeasureresulttype.setCreateTime(now);
        newmeasureresulttype.setLastEditTime(now);
        JSONObject jsonObject = new JSONObject();
        if (!res.equals("correct")){
            jsonObject.put("operationCode", ERROR);
            jsonObject.put("message",res);
            return jsonObject;
        }

        try {
            newmeasureresulttypeMapper.insertSelective(newmeasureresulttype);
            jsonObject.put("operationCode", SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("operationCode", ERROR);
        }
        return jsonObject;
    }

    @Override
    public Object updateMeasureResult(Newmeasureresulttype newmeasureresulttype) {
        LocalDateTime now = LocalDateTime.now();
        String res = RangeUtil.isRangeCorrect(newmeasureresulttype.getRangeMath());
        JSONObject jsonObject = new JSONObject();
        if (!res.equals("correct")){
            jsonObject.put("operationCode", ERROR);
            jsonObject.put("message",res);
            return jsonObject;
        }
        try {
            newmeasureresulttypeMapper.updateByPrimaryKeySelective(newmeasureresulttype);
            jsonObject.put("operationCode", SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("operationCode", ERROR);
        }
        return jsonObject;
    }

    @Override
    public Object disuseMeasureResult(Integer mKey) {
        LocalDateTime now = LocalDateTime.now();
        JSONObject jsonObject = new JSONObject();
        Newmeasureresulttype newmeasureresulttype = new Newmeasureresulttype(mKey, "Y", now);
        int isSuccess = newmeasureresulttypeMapper.updateByPrimaryKeySelective(newmeasureresulttype);
        if (isSuccess == 1) {
            jsonObject.put("operationCode", SUCCESS);
        }else{
            jsonObject.put("operationCode", ERROR);
        }
        return jsonObject;
    }

    @Override
    public List<Newmeasureresulttype> getMeasureResultInfo(Integer preNum) {
        List<Newmeasureresulttype> list = newmeasureresulttypeMapper.getMeasureResultInfo(preNum);
        return list;
    }

    @Override
    public Newmeasuresystem getFatherPreNum(Integer preNum) {
        return newmeasuresystemMapper.selectByPrimaryKey(preNum);
    }

    @Override
    public Newmeasureresulttype getFatherPreNumOnSix(Integer preNum) {
        return newmeasureresulttypeMapper.selectByPrimaryKey(preNum);
    }
}
